package com.yunhe.rentcars.pro.service.car.impl;

import com.yunhe.rentcars.commons.annoaction.Log;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.pro.dao.car.CarInsuranceDao;
import com.yunhe.rentcars.pro.dao.car.CarsDao;
import com.yunhe.rentcars.pro.entity.car.CarInsurance;
import com.yunhe.rentcars.pro.entity.rent.Cars;
import com.yunhe.rentcars.pro.service.car.CarInsuranceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @author maliang
 * @version V1.0
 * @date 2017/11/23 0023 15:00
 * @Description: 车辆保险业务层
 */
@Service
public class CarInsuranceServiceImpl implements CarInsuranceService {

    @Autowired
    private CarInsuranceDao carInsuranceDao;
    @Autowired
    private CarsDao carsDao;

    @Override
    @Log(value = "添加保险业务",type = 1)
    public void save(CarInsurance carInsurance) {
        carInsuranceDao.save(carInsurance);
        carstate(carInsurance);
    }

    @Override
    @Log(value = "批量删除保险记录",type = 2)
    public void delBatch(String ids) {
        String[] split = ids.split(",");
        carInsuranceDao.delBatch(split);
    }

    @Override
    @Log(value = "修改保险记录",type = 3)
    public void update(CarInsurance carInsurance) {
        carInsuranceDao.update(carInsurance);
        carstate(carInsurance);
    }

    @Override
    public Pageable<CarInsurance> find(CarInsurance carInsurance, Integer offset, Integer limit) {
        Pageable<CarInsurance> pageable = new Pageable<>();
        Long total = carInsuranceDao.findCount(carInsurance);
        List<CarInsurance> rows = null;
        if(total > 0){
            rows = carInsuranceDao.findList(carInsurance.getCarcode(),carInsurance.getValiddate(),carInsurance.getInvaliddate(),offset,limit);
        }
        pageable.setTotal(total.intValue());
        pageable.setRows(rows);
        return pageable;
    }

    @Override
    public CarInsurance findOne(Long recordid) {
        return carInsuranceDao.findOne(recordid);
    }

    /**
     * 更改车辆状态
     * @param carInsurance
     */
    public void carstate(CarInsurance carInsurance){
        Cars cars = new Cars();
        cars.setCarcode(carInsurance.getCarcode());
        cars.setInsurer(carInsurance.getCompany()); // 投保单位
        cars.setInsurancetime(carInsurance.getValiddate()); // 保险生效
        cars.setInsuranceend(carInsurance.getInvaliddate()); // 保险到期
        carsDao.carState(cars);
    }
}
